package cool.tianyou.blue.bird.model;

import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;

import cool.tianyou.blue.bird.util.RSAUtils;
import lombok.Data;

/**
 * 传输服务的通用消息体
 */
@Data
public class CommonMessage {


    /**
     * 消息类型
     */

    private String type;
    private Date time;
    private Boolean logSaveToLogList;
    private List<Integer> pushedDeviceIds = new ArrayList<>();

    public CommonMessage setTime(Date time) {
        this.time = time;
        return this;
    }


    /**
     * 消息的唯一id
     */
    private String messageUuid;
    /**
     * 非加密数据
     */
    private Map<String, Object> nonEncrypted = new HashMap<>();
    /**
     * 设备id
     */
    private Integer targetDeviceId;
    /**
     * 加密的数据
     */
    private String secret;
    private final Map<String, Object> encrypted = new HashMap<>();


    /**
     * 生成 消息UUID
     *
     * @return this
     */
    public CommonMessage genUuid() {
        this.messageUuid = UUID.randomUUID().toString().replace("-", "").toUpperCase(Locale.ROOT);
        return this;
    }

    public CommonMessage() {
        this.genUuid();
    }

    private NotificationData notificationData;

    public CommonMessage setNotification(NotificationData notification) {
        this.notificationData = notification;
        return this;
    }

    public Log genLog() {
        return new Log(notificationData, this.time, logSaveToLogList == null || logSaveToLogList);
    }

    public NotificationData getNotificationData() {
        return notificationData;
    }

    public void setNotificationData(NotificationData notificationData) {
        this.notificationData = notificationData;
    }


    public CommonMessage(Integer targetDeviceId) {
        this.targetDeviceId = targetDeviceId;
    }

    /**
     * 添加非加密内容
     *
     * @return
     */
    public CommonMessage putNonEncryptedData(String key, Object value) {
        this.nonEncrypted.put(key, value);
        return this;
    }

    /**
     * 添加加密内容
     *
     * @return
     */
    public CommonMessage putEncryptedData(String key, Object value) {
        this.encrypted.put(key, value);
        return this;
    }

    /**
     * 生成分发的数据
     *
     * @param devices 所有的设备
     * @return 设备id和消息对应的内容
     */
    public Map<String, CommonMessage> genDispatchData(List<Device> devices) {
        String secret = JSONObject.toJSONString(encrypted);
        Map<String, CommonMessage> messages = new HashMap<>();
        for (Device device : devices) {
            CommonMessage message = new CommonMessage(device.getId());
            message.secret = RSAUtils.encryptedDataOnJava(secret, device.getPublicKey());
            message.messageUuid = messageUuid;
            message.nonEncrypted = nonEncrypted;
            message.type = type;
            message.time = time;
            messages.put(String.valueOf(message.targetDeviceId), message);
        }
        return messages;
    }


}
